﻿using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FoodManager
{
    public partial class frBaoCaoDoanhThu : Form
    {
        public frBaoCaoDoanhThu()
        {
            InitializeComponent();
        }

        private void btnThongke_Click(object sender, EventArgs e)
        {
            db.tblkhachhangDataTable current = new db.tblkhachhangDataTable();
            int idkhachhang=0;
            if (cbKhachHang.Text.ToLower() == "tất cả")
            {
                tblkhachhangTableAdapter.Fill(current);
            }
            else
            {
                if (cbKhachHang.SelectedValue != null)
                {
                    idkhachhang=Convert.ToInt32(cbKhachHang.SelectedValue);
                    tblkhachhangTableAdapter.FillById(current,idkhachhang);
                }
                else
                {
                    MessageBox.Show("Bạn nhập sai khách sạn!");
                }
            }
            chartControl1.Series.Clear();
            dbTableAdapters.tblordersTableAdapter orderda = new dbTableAdapters.tblordersTableAdapter();
            dbTableAdapters.tblorderdetailsTableAdapter orderdetailda = new dbTableAdapters.tblorderdetailsTableAdapter();
            foreach (db.tblkhachhangRow row in current.Rows)
            {
                Series khachhang = new Series(row.ten, ViewType.StackedArea);
                db.tblordersDataTable orders = orderda.GetDataByNgayGiaoAndIDKhachHang(dtTuNgay.Value,dtDenNgay.Value, row.id);
                foreach (db.tblordersRow o in orders.Rows)
                {
                    SeriesPoint p=new SeriesPoint(new DateTime(o.ngaygiao.Year,o.ngaygiao.Month,o.ngaygiao.Day));
                    double v = Convert.ToDouble(orderdetailda.GetTotalPriceOrder(o.id));
                    p.Values = new double[] { v };
                    khachhang.Points.Add(p);
                }
                chartControl1.Series.Add(khachhang);
            }
            
            dbTableAdapters.tblThongKeDoanhThuTableAdapter dadoanhthu = new dbTableAdapters.tblThongKeDoanhThuTableAdapter();
            db.tblThongKeDoanhThuDataTable data = dadoanhthu.GetDataDateAndIdKhachHang(dtTuNgay.Value,dtDenNgay.Value, idkhachhang);
            data.Columns.Add("thanhtien");
            foreach (db.tblThongKeDoanhThuRow row in data.Rows)
            {
                double v = Convert.ToDouble(orderdetailda.GetTotalPriceOrder(row.id));
                row["thanhtien"] = v;
            }
            gridControl1.DataSource = data;
        }

        private void btnXuatExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog d = new SaveFileDialog();
            d.FileName = "bao cao doanh thu.xlsx";
            d.Filter = "Excel2007|*.xlsx|All files (*.*)|*.*";
            if (d.ShowDialog() == DialogResult.OK)
            {
                gridControl1.ExportToXlsx(d.FileName);
            }
        }

        private void frBaoCaoDoanhThu_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db.tblkhachhang' table. You can move, or remove it, as needed.
            this.tblkhachhangTableAdapter.Fill(this.db.tblkhachhang);
            // TODO: This line of code loads data into the 'db.tblkhachhang' table. You can move, or remove it, as needed.
            this.tblkhachhangTableAdapter.Fill(this.db.tblkhachhang);

        }
    }
}
